<?php declare(strict_types=1);


namespace App\Rpc\Validator;

use App\Rpc\Model\Dao\UserCaptainDao;
use App\Rpc\Model\Dao\UserCarrierDao;
use Swoft\Validator\Annotation\Mapping\IsInt;
use Swoft\Validator\Annotation\Mapping\IsString;
use Swoft\Validator\Annotation\Mapping\Length;
use Swoft\Validator\Annotation\Mapping\Validator;
use Swoft\Validator\Exception\ValidatorException;
use Swoft\Validator\Contract\ValidatorInterface;
use Swoft\Bean\BeanFactory;
use App\Rpc\Model\Dao\UserDao;
use App\Lib\Error\ErrorConstant;

/**
 * Class UserCarrierValidator
 *
 * @since 2.0
 *
 * @Validator(name="UserCarrierValidator")
 *
 * @package App\Validator
 */
class UserCarrierValidator implements ValidatorInterface
{
    /**
     * @IsString(message="item_id类型不正确")
     * @var string
     */
    protected $item_id;

    /**
     * @IsString(message="账号类型不正确")
     * @Length(min=1, max=32, message="账号信息不合法")
     * @var string
     */
    protected $account;

    /**
     * @IsString(message="密码类型不正确")
     * @Length(min=32, max=32, message="密码格式不正确")
     * @var string
     */
    protected $password;

    /**
     * @IsString(message="盐匙不正确")
     *
     * @var string
     */
    protected $salt;

    /**
     * @IsString(message="承运商id类型不正确")
     * @Length(min=32,max=32,message="承运商id不正确")
     * @var string
     */
    protected $carrier_id;
    /**
     * @IsString(message="登录token不正确")
     * @Length(min=32, max=32, message="token格式不正确")
     * @var string
     */
    protected $token;

    /**
     * @IsInt(message="冻结状态类型不正确")
     *
     * @var int
     */
    protected $frozen_state;

    /**
     * @IsInt(message="创建时间不合法")
     *
     * @var int
     */
    protected $created_at;

    /**
     * @IsInt(message="更新时间不合法")
     *
     * @var int
     */
    protected $updated_at;

    /**
     * @param array $data
     * @param array $params
     * @return array
     * @throws ValidatorException
     */
    public function validate(array $data, array $params): array
    {
        $op = $params['op'] ?? null;
        $valid_item_id = $data['item_id'] ?? null;
        $password = $data['password'] ?? null;
        $account = $data['account'] ?? null;

        if (empty($valid_item_id) && $op == 'add') {
            throw new ValidatorException("唯一item_id必须填写", ErrorConstant::VALIDATE_ERROR_CODE);
        }

        if ($op == 'add' && strlen($valid_item_id) != 32) {
            throw new ValidatorException("唯一item_id格式不正确", ErrorConstant::VALIDATE_ERROR_CODE);
        }

        //验证是否存在
        if ($op == 'add' && !$account) {
            throw new ValidatorException("请输入登录账号", ErrorConstant::VALIDATE_ERROR_CODE);
        }
        if ($account) {
            //验证账号是否唯一
            $UserDao = BeanFactory::getBean(UserCarrierDao::class);
            $first = $UserDao->getByWhere(['account' => $account]);
            if ($first) {
                throw new ValidatorException("账号重复请重新输入", ErrorConstant::VALIDATE_ERROR_CODE);
            }
        }

        //验证是否存在
        if ($op == 'add' && !$password) {
            throw new ValidatorException("请输入登录密码", ErrorConstant::VALIDATE_ERROR_CODE);
        }

        return $data;
    }
}
